import Vue from 'vue'

const app = {
  state: {
    version: '0.0.1',
    author: 'javazz',
    new: {
    }
  },
  mutations: {
    SET_VERSION (state) {
      let arr = state.version.split('.')
      arr[2]++
      if (arr[2] >= 10) {
        arr[2] = 0
        arr[1]++
      }
      if (arr[1] >= 10) {
        arr[1] = 0
        arr[0]++
      }
      let str = `${arr[0]}.${arr[1]}.${arr[2]}`
      state.version = str
    },
    SET_AUTHOR (state, data) {
      // console.log(data)
      // state.author = `java${data.name}`
      // state.new = { ...state.new, name: data.name }
      console.log(arguments)
      Vue.set(state, 'newProp', data.name)
      // state = { ...state, a: data.name }
      // state.new = 'new'
    }
  },
  actions: {
    checkout ({ commit, state }, products) {
      // 把当前购物车的物品备份起来
      const savedCartItems = [...state.cart.added, products]
      console.log(savedCartItems)
      // 发出结账请求，然后乐观地清空购物车
      commit('increment')
      // 购物 API 接受一个成功回调和一个失败回调
    }
  }
}

export default app
